package com.leyou.auth.service;


import com.leyou.auth.domain.SysRole;
import com.leyou.auth.domain.SysUser;
import com.leyou.auth.mapper.UserMapper;
import com.leyou.common.utils.BeanHelper;
import com.leyou.user.client.UserClient;
import com.leyou.user.dto.UserDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserClient userClient;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        //分割获取表标识
        String status = s.split("&")[1];
        int status1 = Integer.parseInt(status);
        //判断用户来源，查询不同的数据库
        if (status1 ==1){
            SysUser user = userMapper.findByName(s.split("&")[0]);
            return user;
        }else {
            UserDTO userDTO = userClient.findByUserName(s.split("&")[0]);
            SysUser user = BeanHelper.copyProperties(userDTO, SysUser.class);
            return user;
        }
    }
}
